package test.bytes;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.SortedMap;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.collections4.map.HashedMap;
import org.junit.Test;

import com.sun.org.apache.xerces.internal.xs.datatypes.ByteList;

import oracle.net.aso.e;

public class GetByteFromUnicodeAndUtf {
	/**
	 * java中String默认获取的字节数组是根据编译器的编码方式来的
	 *  也可以手动自动字节方式
	 * @throws UnsupportedEncodingException
	 */
	@Test
	public void testUtf8() throws UnsupportedEncodingException {
		System.out.println("utf-8");
		byte[] utf8Bytes = "好".getBytes("utf-8");
		System.out.println(utf8Bytes.length);
		String utf8HexString = Hex.encodeHexString(utf8Bytes);
		System.out.println(utf8HexString);
	}
	/**
	 * java获取unicode字节方式的16进制编码
	 * 从char
	 */
	@Test
	public void testUnicodeHex() {
		char c = '好';
		System.out.println(Integer.toBinaryString(c));
		byte high = (byte) (c>>8) ;
		System.out.println(Integer.toBinaryString(high));
		byte low = (byte) (c&0b11111111);
		System.out.println(Integer.toBinaryString(low));
	}
	/**
	 * java获取unicode字节数组，从char
	 */
	@Test
	public void testUnicode() {
		System.out.println("unicode");
		char c = '好';
		System.out.println("16进制："+Integer.toHexString(c));
		System.out.println("2进制："+Integer.toBinaryString(c));
		System.out.println("8进制："+Integer.toOctalString(c));
	}
	/**
	 * 打印charset编码的别名
	 * @throws Exception
	 */
	@Test
	public void testPrintChar() throws Exception {

		SortedMap<String,Charset>  map = Charset.availableCharsets();
		map.entrySet().stream().peek(e -> System.out.println(e.getKey()))
		.forEach(e -> System.out.println(e.getValue()));
		for (String alias : map.keySet())
		{
			System.out.println(alias);
		}
	}
	
	
}
